<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body {
            background-color: #fff;
        }
    </style>
</head>

<body>
    <button id="btn">开关灯</button>
    <script>
        window.onload = function () {
            const btn = document.querySelector('#btn');
            const body = document.querySelector('body');
            let num = 0;
            btn.addEventListener('click', function () {
                if (num == 0) {
                    body.style.backgroundColor = '#000';
                    num = 1;
                }
                else {
                    body.style.backgroundColor = '#fff';
                    num = 0;
                }
            })
        }
    </script>
</body>

</html>